`
https://leetcode.cn/problems/add-two-numbers-ii/
`

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * }
 */
/**
 * @param {ListNode} l1
 * @param {ListNode} l2
 * @return {ListNode}
 */
var addTwoNumbers = function (l1, l2) {
  // 用栈实现翻转链表
  const stk1 = []
  while (l1) {
    stk1.push(l1.val)
    l1 = l1.next
  }
  const stk2 = []
  while (l2) {
    stk2.push(l2.val)
    l2 = l2.next
  }

  // 虚拟结点
  const dummy = {
    next: null
  }

  // 记录进位
  let carry = 0

  while (stk1.length || stk2.length || carry > 0) {
    // 获取当前位的值
    let val = carry
    if (stk1.length) val += stk1.pop()
    if (stk2.length) val += stk2.pop()
    // 处理进位
    carry = Math.floor(val / 10)
    val %= 10
    const node = new ListNode(val)
    node.next = dummy.next
    dummy.next = node
  }

  return dummy.next
};